package app.dev.ems.config.database;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import app.dev.ems.support.annotation.processing.ModelAnnotationProcessor;

public class SchemaHelper {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	private String mappingDirectoryLocations;
	private String driverClassName;
	private String url;
	private String username;
	private String password;
	private String hibernateDialect;
	private ModelAnnotationProcessor modelAnnotationProcessor;
	
	public void createSchema() {
		Configuration configuration = new Configuration();
		configuration.setProperty("hibernate.connection.driver_class", driverClassName);
        configuration.setProperty("hibernate.connection.url", url);
        configuration.setProperty("hibernate.connection.username", username);
        configuration.setProperty("hibernate.connection.password", password);
        configuration.setProperty("hibernate.dialect", hibernateDialect);
        for(Class<?> clazz : modelAnnotationProcessor.getModels()) {
        	configuration.addAnnotatedClass(clazz);
        }
        logger.info("Begin database schema creation.");
        new SchemaUpdate(configuration).execute(true, true);
        logger.info("End database schema creation");
	}

	public String getMappingDirectoryLocations() {
		return mappingDirectoryLocations;
	}

	public void setMappingDirectoryLocations(String mappingDirectoryLocations) {
		this.mappingDirectoryLocations = mappingDirectoryLocations;
	}

	public String getDriverClassName() {
		return driverClassName;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getHibernateDialect() {
		return hibernateDialect;
	}

	public void setHibernateDialect(String hibernateDialect) {
		this.hibernateDialect = hibernateDialect;
	}

	public ModelAnnotationProcessor getModelAnnotationProcessor() {
		return modelAnnotationProcessor;
	}

	public void setModelAnnotationProcessor(ModelAnnotationProcessor modelAnnotationProcessor) {
		this.modelAnnotationProcessor = modelAnnotationProcessor;
	}

}
